//获取微信Access token
const axios = require('axios')

const AppId = '8QKL9paSzdCrKp1Kwq'
const APPSECRET = 'ULEnWaLkYppeYNbrH7fNlwXbZ7JfLM1l'
const accessTokenPCUrl = ` https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${AppId}&secret=${APPSECRET}`
const qrUrl = `https://mp.weixin.qq.com/cgi-bin/showqrcode`

const getAccessToken = () => {
    return axios({
        method: 'get',
        url: accessTokenPCUrl
    })
}

const getTicket = (token) => {
    return axios({
        method: 'post',
        url: `https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=${token}`,
        data: {
            expire_seconds:  60 * 2,
            action_name: "QR_SCENE",
            action_info: { scene: { scene_id: 123 } }
        }
    })
}

// 获取二维码url
const wxchatLogin = {
    getOR: async () => {
        const token = await getAccessToken().data.access_token
        const ticket = await getTicket(token)
        return { qrcodeUrl: `${qrUrl}?ticker=${ticket}`, ticket: ticket }
    }
}
module.exports = wxchatLogin